<?php

class LiteCallback implements LiteCallable {

	/**
	 * Devuelve verdadero si <code>$callback</code> es un callback
	 * 
	 * @param callback $callback
	 * @return boolean
	 */
	static function isCallable($var){
		return $var instanceof LiteCallable || is_callable($var);
	}

	/**
	 * @var callback
	 */
	private $callback;

	/**
	 * @param callback $callback
	 * @param array $params
	 */
	function __construct($callback){
		
		if(!self::isCallable($callback)) {
			throw new LiteException('Invalid callback');
		}
		
		$this->callback = $callback;
	}

	/**
	 * Crea una copia del callback y sus argumentos
	 * 
	 * <code>
	 * $cb = new LiteCallback('phpinfo');
	 * $cb->call(); // phpinfo();
	 *
	 * $fn = $cb->getCallback();
	 * $fn(); // phpinfo();
	 * </code>
	 * @return callback
	 */
	function getCallback(){
		return $this->callback;
	}

	/**
	 * Invoca el callback
	 * 
	 * @return mixed
	 */
	function call(){
		return $this->callback instanceof LiteCallable ? $this->callback->call() : $this->callback();
	}
}
